package org.example.hrpc.example;

import org.example.hrpc.common.generic.GenericService;
import org.example.hrpc.config.reference.ReferenceConfig;
import org.example.hrpc.rpc.core.AppResponse;

import java.io.IOException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class HelloClientGeneric {
    public static void main(String[] args) throws IOException {
        ReferenceConfig<GenericService> referenceConfig = new ReferenceConfig<>();
        referenceConfig.setInterfaceName("org.example.hrpc.example.HelloService");
        referenceConfig.setBindToHost("127.0.0.1");
        referenceConfig.setBindToPort(30000);
        referenceConfig.setGeneric("true");

        referenceConfig.setMethodName("hello");
        Class[] parmTyps = new Class[]{String.class};
        referenceConfig.setParameterTypes(parmTyps);
        Object[] arguments = new Object[]{"World"};
        referenceConfig.setArguments(arguments);

        referenceConfig.init();
//        HelloService helloService = referenceConfig.get();
//        System.out.println(helloService);
//        String res = helloService.hello("World");
        GenericService genericService = referenceConfig.get();
        // sync
//        Object res = genericService.invoke("org.example.hrpc.example.HelloService", "hello",
//                new String[]{Object.class.getName()}, new Object[]{"World!"});
//        System.out.println((String)res);
        // async
        CompletableFuture<AppResponse> res = (CompletableFuture<AppResponse>) genericService.invoke("org.example.hrpc.example.HelloService", "hello",
                new String[]{Object.class.getName()}, new Object[]{"World!"});
        try {
            System.out.println(res.get().recreate());
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
        System.in.read();
    }
}
